//
//  ProductTableView.m
//  tableView联动
//
//  Created by 程建良 on 16/8/20.
//  Copyright © 2016年 程建良. All rights reserved.
//

#import "ProductTableView.h"
#import "ProductModel.h"
#import "ProductViewCell.h"


//indexPath的位置
#define INDEXPATHORIGIN(TABVIEW,A,B) [TABVIEW rectForRowAtIndexPath:[NSIndexPath indexPathForRow:A inSection:B]].origin

//cell的行高
#define cellHeight 80


@interface ProductTableView()<UITableViewDataSource,UITableViewDelegate>

@end
@implementation ProductTableView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[ProductViewCell class] forCellReuseIdentifier:@"product"];
        
        
        
        //接受通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sectionChange:) name:@"sectionChange" object:nil];
        self.showsVerticalScrollIndicator = NO;
    }
    return self;
}

-(void)setTabArray:(NSArray<TabModel *> *)tabArray
{
    _tabArray = tabArray;
    
    [self reloadData];
}



#pragma mark --通知实现--
-(void)sectionChange:(NSNotification*)notification
{
    NSString * index = notification.object;
    /*
    [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index.integerValue] animated:YES scrollPosition:UITableViewScrollPositionTop];
    */
    /*
    CGFloat headHeight = self.sectionHeaderHeight;
    
    NSLog(@"组头高度%f",headHeight);
    */
    
#warning 组头高度=25？？？
    
    CGPoint newContet = CGPointMake(INDEXPATHORIGIN(self, 0, index.integerValue).x, INDEXPATHORIGIN(self, 0, index.integerValue).y - 25);
    
    
    
    [self setContentOffset:newContet animated:YES];
    
    
}
#pragma mark -- UItableViewDataSource --

//1-组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.tabArray.count;
}
//2-行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tabArray[section].products.count;
}
//3-行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"product"];
    
    
    ProductModel *productModel = self.tabArray[indexPath.section].products[indexPath.row];
    
    //取出本地存储的商品购买数量
    [DBManager SearchDataWithModel:productModel];
    
    
    cell.proModel = productModel;
    
    
    return cell;
}
//4-组头内容

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.tabArray[section].name;
}




#pragma mark -- UITableViewDelegate --
//实现product至tap联动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    /*
    NSLog(@"第0组%f",INDEXPATHORIGIN(self, self.tabArray[0].products.count-1, 0).y+cellHeight);
    
    NSLog(@"%f",scrollView.contentOffset.y + 12.5);
    
    NSLog(@"第1组%f",INDEXPATHORIGIN(self, self.tabArray[1].products.count-1, 1).y+cellHeight);
    */
    
#warning 12.5:组头高度的一半??？
    if ( scrollView.contentOffset.y + 12.5 <= INDEXPATHORIGIN(self, self.tabArray[0].products.count - 1, 0).y+cellHeight )
    {
        //发送通知
        [[NSNotificationCenter defaultCenter]postNotificationName:@"offsetChange" object:@(0)];
    }
    
    for (int i = 1; i < self.tabArray.count ; i++)
    {
        if (scrollView.contentOffset.y + 12.5 > INDEXPATHORIGIN(self, self.tabArray[i-1].products.count-1, i-1).y+cellHeight && scrollView.contentOffset.y <= INDEXPATHORIGIN(self, self.tabArray[i].products.count-1, i).y+cellHeight )
        {
            //发送通知
            [[NSNotificationCenter defaultCenter]postNotificationName:@"offsetChange" object:@(i)];
        }
    }
}




//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

#pragma mark --移除监听--
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
